from django.shortcuts import render
from course import models, courseser
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from django.db.models import Q
from django_filters.rest_framework import FilterSet
import django_filters
# Create your views here.


# class SearchCate(FilterSet):
#     ordering = django_filters.OrderingFilter(fields=['id', 'price', 'numbers'])
#
#     class Meta:
#         fields = ['ordering']
#

class Page(PageNumberPagination):
    page_size = 3
    page_size_query_param = 'size'


class Homecate(APIView):
    def get(self, request):
        catelist = models.CateModel.objects.filter(recommend=True, parent=0).order_by('order')
        ser = courseser.CategorySer(catelist, many=True)
        data = ser.data
        for item in data:
            item['son'] = item['son'][0:3]
        return Response({'msg': 'ok', 'data': ser.data})


class CateCourse(APIView):
    def get(self, request):
        parentid = request.query_params.get('pid')
        catelist = models.CateModel.objects.filter(id=parentid).first()
        ser = courseser.CategorySer(catelist)
        sonlist = []
        for item in ser.data["son"]:
            sonlist.append(item["id"])
        homecourse = models.CourseModel.objects.filter(category__in=sonlist, is_home=True).order_by("order")
        coursedata = courseser.CourseSer(homecourse, many=True).data
        datalist = ser.data
        datalist["course"] = coursedata
        return Response(datalist)


class GetParentCate(APIView):
    def get(self, request):
        parentcate = models.CateModel.objects.filter(parent=0)
        ser = courseser.CateSer(parentcate, many=True)
        return Response({"mes": "ok", "data": ser.data})


class GetSon(APIView):
    def get(self, request):
        # 默认的展示所有的三级分类，如果给参数 就是展示当前参数下的二级分类
        parentid = request.query_params.get("pid")
        if parentid:
            # 根据条件拿到数据
            sondata = models.CateModel.objects.filter(parent=parentid)
        else:
            # 展示全部数据
            sondata = models.CateModel.objects.filter(parent__gt=0)  # pid为空时获取所有子类
        ser = courseser.CateSer(sondata, many=True)
        return Response(ser.data)


# 获取所有的课程信息
class GetCourse(ListAPIView):
    queryset = models.CourseModel.objects.filter(is_show=True).order_by("order")
    serializer_class = courseser.CourseSer
    pagination_class = Page
    # filterset_class = SearchCate

    def get_queryset(self):
        query = Q(is_show=True)
        cid = self.request.query_params.get("cid")
        did = self.request.query_params.get("did")
        level = self.request.query_params.get("level")
        orderdata = self.request.query_params.get("ordering")
        if level:
            query = query & Q(level=level)
        if cid:
            query = query & Q(category=cid)
        else:
            if did:
                son = models.CateModel.objects.filter(parent=did).order_by("order")
                sondata = []
                for i in son:
                    sondata.append(i.id)
                query = query & Q(category__in=sondata)
        if orderdata:
            queryset = models.CourseModel.objects.filter(query).order_by("order", orderdata)
        else:
            queryset = models.CourseModel.objects.filter(query).order_by("order")
        return queryset


class GetDetail(APIView):
    def get(self, request):
        id = request.query_params.get("id")
        data = models.CourseModel.objects.filter(id=id).first()
        ser = courseser.CourseSer(data)
        return Response(ser.data)
